package com.example.myapplication.found.model;

import com.example.myapplication.found.IndustryFound;
import com.example.myapplication.found.bean.KDJOrderBean;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockOrderBean;
import com.example.myapplication.found.contract.CghCallback;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author: rongfeng
 * @date: 2022/3/4
 * @description
 */
public class WeekCgh extends BaseData {
    Map<Integer, Float> map = new HashMap<>();

    public WeekCgh(StockBean stockBean, StockBean weekStockBean) {
        super(stockBean, weekStockBean);
    }

    CghCallback cghCallback;

    @Override
    public void start() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        boolean isBuy = false;
        for (StockKlineBean stockKlineBean : stockBean.klineList) {
            Date datet = null;
            try {
                datet = format.parse(stockKlineBean.date);
                cal.setTime(datet);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
            if (w < 0) {
                w = 0;
            }
            stockKlineBean.week = w;
            Float value = map.get(w);
            if (value == null) {
                value = 0f;
            }
            value = value + stockKlineBean.chg;
            map.put(w, value);


            if (w == 5) {
                isBuy = deal(stockKlineBean);
            } else if (w == 2 && isBuy) {
//                isBuy = false;
//                deal(stockKlineBean);
            }
        }
        if (IndustryFound.isTest) {
            Iterator<Map.Entry<Integer, Float>> it1 = map.entrySet().iterator();
            while (it1.hasNext()) {
                Map.Entry<Integer, Float> entry = it1.next();
                System.out.println("周: " + entry.getKey() + "  涨跌： " + entry.getValue());
            }
        }

        statistics();

    }

    private boolean deal(StockKlineBean stockKlineBean) {
        float money = 10000;
        money = money + (money * (stockKlineBean.chg / 100));
        StockOrderBean orderBean = new StockOrderBean();
        orderBean.average = money;
        orderBean.zhang = money > 10000;
        orderBean.die = money < 10000;
        orderBean.pin = money == 10000;
        orderBean.money = money;
        orderList.add(orderBean);

        if (IndustryFound.isTest) {
            System.out.println(stockKlineBean.date + " 涨跌 " + stockKlineBean.chg + " money " + money);
        }
        return money < 10000;

    }


    public void statistics() {
        float orderZhang = 0, orderDie = 0, orderPin = 0, orderAvg = 0, money = 0;
        for (StockOrderBean orderBean : orderList) {
            if (orderBean.zhang) {
                orderZhang++;
            }
            if (orderBean.die) {
                orderDie++;
            }
            if (orderBean.pin) {
                orderPin++;
            }
            money = money + orderBean.money;
            orderAvg = orderAvg + orderBean.average;
        }
        float rate = 0;
        KDJOrderBean kdjOrderBean = null;
        if (orderList.size() > 0) {
            rate = (orderZhang / orderList.size()) * 100;
            float moneyAvg = money / orderList.size();
            if (IndustryFound.isTest) {
                System.out.println(fundName + " " + fundCode);
                System.out.println("买入次数 " + orderList.size());
                System.out.println("买入涨 " + orderZhang + " 跌 " + orderDie + "  平 " + orderPin + " 胜率 " + rate + "%");
                System.out.println("平均剩余金额 " + moneyAvg);
                System.out.println();
            }
            kdjOrderBean = new KDJOrderBean();
            kdjOrderBean.moneyAvg = moneyAvg;
            kdjOrderBean.rate = rate;
            kdjOrderBean.orderZhang = orderZhang;
            kdjOrderBean.orderDie = orderDie;
            kdjOrderBean.orderPin = orderPin;
            kdjOrderBean.orderList = orderList;
            kdjOrderBean.name = fundName;
            kdjOrderBean.code = fundCode;
        }
        if (cghCallback != null) {
            cghCallback.cghCallback(map, kdjOrderBean);
        }
    }

    public CghCallback getCghCallback() {
        return cghCallback;
    }

    public void setCghCallback(CghCallback cghCallback) {
        this.cghCallback = cghCallback;
    }
}
